package com.joysuch.wwyt.bp.controller;

import com.joysuch.wwyt.bp.bean.*;
import com.joysuch.wwyt.bp.entity.*;
import com.joysuch.wwyt.bp.service.BpStoreHouseService;
import com.joysuch.wwyt.core.common.ajax.ResultBean;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/bp/house")
@Api(tags = "设备设施管理-仓库")
@Slf4j
public class BpStoreHouseController {

    @Autowired
    private BpStoreHouseService bpStoreHouseService;

    @PostMapping("add")
    @ApiOperation(value = "增加仓库")
    @RequiresPermissions("bp:house:add")
    public ResultBean add(@RequestBody @Valid BpStoreHouseAddBean bpStoreHouse) {
        BpStoreHouse house = bpStoreHouseService.add(bpStoreHouse);
        return ResultBean.success(house);
    }

    @PostMapping("edit")
    @ApiOperation(value = "修改仓库")
    @RequiresPermissions("bp:house:edit")
    public ResultBean edit(@RequestBody @Valid BpStoreHouseAddBean bpStoreHouse) {
        BpStoreHouse house = bpStoreHouseService.edit(bpStoreHouse);
        return ResultBean.success(house);
    }

    @GetMapping("delete")
    @ApiOperation(value = "删除仓库")
    @RequiresPermissions("bp:house:delete")
    public ResultBean delete(Long id) {
        bpStoreHouseService.delete(id);
        return ResultBean.defaultSuccessResult();
    }

    @GetMapping("getById")
    @ApiOperation(value = "查看仓库")
    public ResultBean getById(Long id) {
        try {
            BpStoreHouseAddBean byId = bpStoreHouseService.getById(id);
            return ResultBean.success(byId);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "查看失败");
        }
    }

    @GetMapping("getList")
    @ApiOperation(value = "查看仓库列表")
    public ResultBean getList(Integer typeFlag) {
        List<BpStoreHouseListBean> list = bpStoreHouseService.getList(typeFlag);
        return ResultBean.success(list);
    }

    //待删除
    @GetMapping("pageMhPoint")
    @ApiOperation(value = "分页展示监测点")
    public ResultBean pageMhPoint(Pageable pageable, Long parentId) {
        return bpStoreHouseService.pageMhPoint(pageable, parentId);
    }

    @PostMapping("addType")
    @ApiOperation(value = "增加物资类型")
    @RequiresPermissions("bp:house:type")
    public ResultBean addType(@RequestBody BpStoreMaterialTypeOrUnitBean bean) {
        BpStoreMaterialType type = bpStoreHouseService.addType(bean);
        return ResultBean.success(type);
    }

    @PostMapping("updateType")
    @ApiOperation(value = "修改物资类型")
    @RequiresPermissions("bp:house:type")
    public ResultBean updateType(@RequestBody BpStoreMaterialTypeOrUnitBean bean) {
        BpStoreMaterialType type = bpStoreHouseService.updateType(bean);
        return ResultBean.success(type);
    }

    @GetMapping("deleteTypeById")
    @ApiOperation(value = "删除物资类型")
    @RequiresPermissions("bp:house:deleteTypeById")
    public ResultBean deleteTypeById(Long id) {
        try {
            bpStoreHouseService.deleteTypeById(id);
            return ResultBean.success("");
        } catch (Exception e) {
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("pageType")
    @ApiOperation(value = "分页展示物资类型")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageable", value = "分页参数", required = false),
            @ApiImplicitParam(name = "typeFlag", value = "0物资仓库;1危化品仓库",  required = false)
    })
    public ResultBean pageType(Pageable pageable,Integer typeFlag) {
        if (typeFlag == null) {
            typeFlag = 0;
        }
        return bpStoreHouseService.pageType(pageable,typeFlag);
    }

    @PostMapping("addUnit")
    @ApiOperation(value = "增加物资单位")
    @RequiresPermissions("bp:house:unit")
    public ResultBean addUnit(@RequestBody BpStoreMaterialTypeOrUnitBean bean) {
        BpStoreMaterialUnit type = bpStoreHouseService.addUnit(bean);
        return ResultBean.success(type);
    }

    @PostMapping("updateUnit")
    @ApiOperation(value = "修改物资单位")
    @RequiresPermissions("bp:house:unit")
    public ResultBean updateUnit(@RequestBody BpStoreMaterialTypeOrUnitBean bean) {
        BpStoreMaterialUnit type = bpStoreHouseService.updateUnit(bean);
        return ResultBean.success(type);
    }

    @GetMapping("deleteUnitById")
    @ApiOperation(value = "删除物资单位")
    @RequiresPermissions("bp:house:deleteUnitById")
    public ResultBean deleteUnitById(Long id) {
        try {
            bpStoreHouseService.deleteUnitById(id);
            return ResultBean.success("");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("pageUnit")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageable", value = "分页参数", required = false),
            @ApiImplicitParam(name = "typeFlag", value = "0物资仓库;1危化品仓库",  required = false)
    })
    @ApiOperation(value = "分页展示物资单位")
    public ResultBean pageUnit(Pageable pageable,Integer typeFlag) {
        if (typeFlag == null) {
            typeFlag = 0;
        }
        return bpStoreHouseService.pageUnit(pageable, typeFlag);
    }

    @GetMapping("getUnitList")
    @ApiOperation(value = "获取物资单位列表")
    public ResultBean getUnitList() {
        List<BpStoreMaterialTypeOrUnitListBean> list = bpStoreHouseService.getUnitList();
        return ResultBean.success(list);
    }

    @GetMapping("getTypeList")
    @ApiOperation(value = "获取物资类型列表")
    public ResultBean getTypeList() {
        List<BpStoreMaterialTypeOrUnitListBean> list = bpStoreHouseService.getTypeList();
        return ResultBean.success(list);
    }

    @PostMapping("addMaterial")
    @ApiOperation(value = "增加物资")
    @RequiresPermissions("bp:house:addMaterial")
    public ResultBean addMaterial(@RequestBody BpStoreMaterialBean bean) {
        try {
            BpStoreMaterial m = bpStoreHouseService.addMaterial(bean);
            return ResultBean.success(m);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "新增失败");
        }
    }

    @PostMapping("updateMaterial")
    @ApiOperation(value = "修改物资")
    @RequiresPermissions("bp:house:updateMaterial")
    public ResultBean updateMaterial(@RequestBody BpStoreMaterialBean bean) {
        try {
            BpStoreMaterial m = bpStoreHouseService.updateMaterial(bean);
            return ResultBean.success(m);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "修改失败");
        }
    }

    @GetMapping("getMaterialById")
    @ApiOperation(value = "查看物资")
    public ResultBean getMaterialById(Long id) {
        try {
            BpStoreMaterialBean bean = bpStoreHouseService.getMaterialById(id);
            return ResultBean.success(bean);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "查看失败");
        }
    }

    @GetMapping("deleteMaterial")
    @ApiOperation(value = "删除物资")
    @RequiresPermissions("bp:house:deleteMaterial")
    public ResultBean deleteMaterial(Long id) {
        try {
            bpStoreHouseService.deleteMaterial(id);
            return ResultBean.success("");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return ResultBean.fail(101, "删除失败");
        }
    }

    @GetMapping("pageMaterial")
    @ApiOperation(value = "分页展示物资")
    public ResultBean pageMaterial(Pageable pageable, Long huoseId,Integer typeFlag,BpStoreMaterialSearchBean condition) {
        return bpStoreHouseService.pageMaterial(pageable, huoseId,typeFlag,condition);
    }

    @PostMapping("recordMaterial")
    @ApiOperation(value = "出库/入库/盘点")
    @RequiresPermissions("bp:house:recordMaterial")
    public ResultBean recordMaterial(@RequestBody BpStoreMaterialRecordBean bean) {
        BpStoreMaterialRecord m = bpStoreHouseService.recordMaterial(bean);
        return ResultBean.success(m);
    }

    @GetMapping("pageMaterialRecord")
    @ApiOperation(value = "分页展示物资记录")
    public ResultBean pageMaterialRecord(Pageable pageable, Long materialId) {
        return bpStoreHouseService.pageMaterialRecord(pageable, materialId);
    }

    @GetMapping("getMaterialRecord")
    @ApiOperation(value = "查看物资记录")
    public ResultBean getMaterialRecord(Long recordId) {
        BpStoreMaterialRecordBean bean = bpStoreHouseService.getMaterialRecord(recordId);
        return ResultBean.success(bean);
    }
    
    @GetMapping("/chemProductSimpleList")
    @ApiOperation(value = "被仓库使用的危化品下拉列表")
    public ResultBean findchemProductSimpleList(){
        return ResultBean.success(bpStoreHouseService.findchemProductSimpleList());
    }
}
